package com.example.pceb.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pceb.VO.ApplyInformationVO;
import com.example.pceb.entity.*;
import com.example.pceb.mapper.*;
import com.example.pceb.result.Result;
import com.example.pceb.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    InformationMapper informationMapper;
    @Autowired
    SchoolMapper schoolMapper;
    @Autowired
    EnterpriseMapper enterpriseMapper;
    @Autowired
    ApplyInformationMapper applyInformationMapper;
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    QueBankMapper queBankMapper;
    @Autowired
    AdminMapper adminMapper;

    @Override
    public Result<List<Information>> getAllInformation() {
        return Result.success(informationMapper.selectList(null));
    }
    public Result<List<Information>> getPolicy(){
        QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 4);
        return Result.success(informationMapper.selectList(queryWrapper));
    }
    public Result<List<Information>> getBusiness(){
        QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 3);
        return Result.success(informationMapper.selectList(queryWrapper));
    }
    public Result<List<Information>> getRecruit(){
        QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 2);
        return Result.success(informationMapper.selectList(queryWrapper));
    }
    public Result<List<Information>> getCooperation(){
        QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 1);
        return Result.success(informationMapper.selectList(queryWrapper));
    }

    @Override
    public Result<List<Enterprise>> getEnterprise() {
        return Result.success(enterpriseMapper.selectList(null));
    }

    @Override
    public Result<List<School>> getSchool() {
        return Result.success(schoolMapper.selectList(null));
    }

    public Result<List<ApplyInformationVO>> getExamine() {
        List<ApplyInformation> applyInformationList = applyInformationMapper.selectList(null);
        List<ApplyInformationVO> applyInformationVOList = new ArrayList<>();
        for (ApplyInformation applyInformation : applyInformationList) {
            ApplyInformationVO applyInformationVO = new ApplyInformationVO();
            applyInformationVO.setApplyInformation(applyInformation);
            applyInformationVO.setProject(projectMapper.selectById(applyInformation.getProjectId()));
            applyInformationVOList.add(applyInformationVO);
        }
        return Result.success(applyInformationVOList);
    }

    @Override
    public Result<String> delExamine(List<Integer> idList) {
        applyInformationMapper.delete(new QueryWrapper<ApplyInformation>().in("id", idList));
        return Result.success("删除成功！");
    }

    @Override
    public Result<String> qualifiedExamine(int id, int isPass) {
        ApplyInformation applyInformation = applyInformationMapper.selectById(id);
        applyInformation.setIsExamine("1");
        applyInformation.setIsPass(isPass);
        applyInformationMapper.updateById(applyInformation);
        return Result.success("审核成功");
    }

    @Override
    public Result<String> addInformation(Information information) {
        informationMapper.insert(information);
        return Result.success("添加成功！");
    }

    @Override
    public Result<String> delInformation(List<Integer> idList) {
        informationMapper.delete(new QueryWrapper<Information>().in("id", idList));
        return Result.success("删除成功！");
    }

    @Override
    public Result<String> updateInformation(Information information) {
        informationMapper.updateById(information);
        return Result.success("更新成功！");
    }

    @Override
    public Result<String> addStudent(Student student) {
        studentMapper.insert(student);
        return Result.success("添加成功！");
    }

    @Override
    public Result<String> delStudent(List<String> idList) {
        studentMapper.delete(new QueryWrapper<Student>().in("id", idList));
        return Result.success("删除成功！");
    }

    @Override
    public Result<String> updateStudent(Student student) {
        studentMapper.updateById(student);
        return Result.success("更新成功！");
    }

    @Override
    public Result<String> addEnterprise(Enterprise enterprise) {
        enterpriseMapper.insert(enterprise);
        return Result.success("添加成功！");
    }

    @Override
    public Result<String> delEnterprise(List<String> idList) {
        enterpriseMapper.delete(new QueryWrapper<Enterprise>().in("id", idList));
        return Result.success("删除成功！");
    }

    @Override
    public Result<String> updateEnterprise(Enterprise enterprise) {
        enterpriseMapper.updateById(enterprise);
        return Result.success("更新成功！");
    }

    @Override
    public Result<String> addSchool(School school) {
        schoolMapper.insert(school);
        return Result.success("添加成功！");
    }

    @Override
    public Result<String> delSchool(List<Integer> idList) {
        schoolMapper.delete(new QueryWrapper<School>().in("id", idList));
        return Result.success("删除成功！");
    }

    @Override
    public Result<String> updateSchool(School school) {
        schoolMapper.updateById(school);
        return Result.success("更新成功！");
    }

    @Override
    public Result<List<QueBank>> getAllQuestionBank() {
        return Result.success(queBankMapper.selectList(null));
    }

    @Override
    public Result<String> updateAdminAvatar(Integer adminId, String avatarUrl) {
        Admin admin = new Admin();
        admin.setId(adminId);
        admin.setAvatarUrl(avatarUrl);
        int updated = 0;
        try {
            updated = adminMapper.updateById(admin);
        } catch (Exception e) {
            return Result.error("数据库更新头像失败");
        }
        if (updated > 0) {
            return Result.success("头像更新成功");
        } else {
            return Result.error("头像更新失败");
        }
    }

}